<template>
  <div>
    <!--条件筛选-->
    <TopFilter :list="topFilterList" labelWidth="110" :cols="2" @filterChange="changeHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="lxxTimeManageMent"
      :columns="columns"
      :datakey="'records'"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectHandler"
      selectionType="single"
    >
      <template slot="controls">
        <AjaxButton size="small" type="primary" :click="getPersonList">打印详情</AjaxButton>
      </template>
    </FilterTable>
    <BasePrint
      ref="print"
      :data="printData"
      destroyOnClose
      :template="'repair/repairReception/printfinance/printTimeManage'"
    />
    <!-- src/pages/printTemplate/repair/repairReception/printfinance/printTimeManage.vue -->
  </div>
</template>

<script>
/**
 * 时间管理报表
 */
import { dictionary } from '@/utils/dictMixin';
import { getList, getPersonList } from '@/api/repair/workshopManagement/timeManagement/index';
import moment from 'moment';
export default {
  name: 'lxxTimeManageMent',
  components: {},
  mixins: [dictionary],
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      fetchParams: { xhrAbort: true },
      fetchApi: getList,
      printData: {},
      columns: this.createTableColumns(),
      tableData: [],
      pList: [],
      selectedRow: [],
      dataTime: [
        moment()
          .startOf('mouth')
          .format('YYYY-MM-DD'),
        moment().format('yyyy-MM-DD')
      ]
    };
  },
  mounted() {
    // 分页
    this.BaseTable = this.$refs.table.$refs.pageTable;
    // this.topFilterList.find(x => x.fieldName == 'startTime|endTime').initialValue = [moment().startOf('mouth'), moment().format('yyyy-MM-DD')];
    // this.getService();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'RANGE_DATE',
          label: '日期',
          fieldName: 'startTime|endTime',

          initialValue: [
            moment()
              .startOf('month')
              .format('YYYY-MM-DD'),
            moment().format('YYYY-MM-DD')
          ],
          rules: [{ required: true, message: '请选择日期', trigger: 'blur' }]
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '班组',
          dataIndex: 'WORKGROUP_NAME',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '售出时间(h)',
          showOverflowTooltip: true,
          dataIndex: 'LABOUR_TIME',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '出勤时间(h)',
          showOverflowTooltip: true,
          dataIndex: 'DUTY',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '生产时间(h)',
          showOverflowTooltip: true,
          dataIndex: 'TIME',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '利用率(%)',
          showOverflowTooltip: true,
          dataIndex: 'LDLYL',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '效率(%)',
          showOverflowTooltip: true,
          dataIndex: 'LDXL',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '生产率(%)',
          showOverflowTooltip: true,
          dataIndex: 'LDSCL',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '应收工时费(元)',
          showOverflowTooltip: true,
          dataIndex: 'RECEIVEABLE_LABOUR_FEE',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '应收材料费(元)',
          showOverflowTooltip: true,
          dataIndex: 'SALE_PAR_SUM',
          sorter: true,
          filter: true,
          filterType: 'number'
        }
      ];
    },
    selectHandler(val) {
      this.selectedRow = val;
      console.log('this.selectedRow', this.selectedRow);
      if (this.selectedRow) {
        this.printData = this.selectedRow;
        this.printData[0].endTime = this.fetchParams.endTime;
        this.printData[0].startTime = this.fetchParams.startTime;
        this.printData[0].num = Number(this.selectedRow[0].DUTY) - Number(this.selectedRow[0].TIME);
        console.log(this.printData, '666');
      } else {
        return;
      }
    },
    async getPersonList() {
      let params = { sign: 'work', userId: '', workGropId: this.selectedRow[0].WORKGROUP_ID };
      const res = await getPersonList(params);
      this.pList = [];
      for (let i = 0; i < res.data.length; i++) {
        this.pList.push(res.data[i].EMPLOYEE_NAME);
      }
      this.printData[0].person = this.pList.toString();
      // console.log('renyuan', , 'this.pList');
      this.printHandle();
    },
    changeHandle(val) {
      console.log('MDZZ', val);
      val.endTime = val.endTime.split(' ')[0];
      val.startTime = val.startTime.split(' ')[0];
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {},
    printHandle() {
      this.$refs.print.EXCUTE_PRINT();
    }
  }
};
</script>

<style lang="less" scoped></style>
